<?php

/**
 * CatepicController
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class CatepicController extends Zend_Controller_Action {
	
	protected $_pictureModel;
	
	public function init()
	{
	     $authService = new Eleven_Service_Authentication();
	           
	    if ($authService->getIdentity()->role != 'Teacher') {
	        $this->_helper->redirector('index','index');
	    }
	    
		$this->_pictureModel = new Eleven_Model_Picture();
		
		$this->view->cateForm = $this->getCateForm();
		$this->view->controller = $this->_request->controller;
	}
	
	public function indexAction() {
		$catepics = $this->_pictureModel->getCatepics();
        $this->view->assign(array('catepics'=>$catepics));
	}
	
	public function deleteAction() {
		 $deleteInfo = $this->_request->getPost();
        
        foreach ($deleteInfo['deleteIds'] as $key => $val)
        {
           	$this->_pictureModel->deleteCatepicById($key);
        }
        
        return $this->_helper->redirector('index');
	}
	
 	public function getCateForm()
	{
		$urlHelper = $this->_helper->getHelper('url');

        $this->_forms['cate'] = $this->_pictureModel->getForm('pictureCate');
        $this->_forms['cate']->setAction($urlHelper->url(array(
            'controller'	=> 'catepic',
            'action' => 'add'
            )
        ));
        
        $this->_forms['cate']->setMethod('post');
        
        return $this->_forms['cate'];
	}
	
	public function addAction()
	{
	    $request = $this->getRequest();
		if (!$request->isPost()) 
		{
			return $this->_helper->redirector('index');
  		}
  		
  		if (false === $this->_pictureModel->addCate($request->getPost())) 
  		{
  		    $catepics = $this->_pictureModel->getCatepics();
            $this->view->assign(array('catepics'=>$catepics));
     		return $this->render('index');
  		}
  		
  		return $this->_helper->redirector('index');
	}
}

